<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择用户"
          filterable
          clearable
        >
          <el-option
            v-for="dict in userArr"
            :key="dict.id"
            :label="dict.userName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目号" prop="projectCode">
        <el-select
          v-model="queryParams.projectCode"
          placeholder="请选择项目"
          filterable
          clearable
        >
          <el-option
            v-for="dict in projectArr"
            :key="dict.code"
            :label="dict.name"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="动账时间" prop="rzTime">
        <el-date-picker clearable
          v-model="queryParams.rzTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择动账时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="描述" prop="detail">
        <el-input
          v-model="queryParams.detail"
          placeholder="请输入描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:dzLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:dzLog:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:dzLog:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dzLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目号" align="center" prop="projectCode" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.type == '0'">收入</el-tag>
          <el-tag type="success" v-else>支出</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="动账时间" align="center" prop="rzTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rzTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="detail" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:dzLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对账记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出资用户" prop="userId">
          <el-select
            v-model="form.userId"
            placeholder="请选择用户"
            filterable
            clearable
          >
            <el-option
              v-for="dict in userArr"
              :key="dict.id"
              :label="dict.userName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收帐用户" prop="userId">
          <el-select
            v-model="form.szUserId"
            placeholder="请选择用户"
            filterable
            clearable
          >
            <el-option
              v-for="dict in userArr"
              :key="dict.id"
              :label="dict.userName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目" prop="projectCode">
            <el-select
              v-model="form.projectCode"
              placeholder="请选择项目"
              filterable
              clearable
            >
              <el-option
                v-for="dict in projectArr"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              label="0"
            >收入</el-radio>
            <el-radio
              label="1"
            >支出</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="动账时间" prop="rzTime">
          <el-date-picker clearable
            v-model="form.rzTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择动账时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述" prop="detail">
          <el-input
            type="textarea"
            placeholder="请输入描述"
            v-model="form.detail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-loading="btnLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDzLog, getDzLog, delDzLog, addDzLog, updateDzLog } from "@/api/wms/dzLog";
import { listDzProjectXlk } from '@/api/wms/dzProject'
import { listDzInfoXlk } from '@/api/wms/dzInfo'
import offerOrder from '@/views/wms/offerOrder/index.vue'

export default {
  name: "DzLog",
  data() {
    return {
      //按钮加载
      btnLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账记录表格数据
      dzLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        projectCode: null,
        money: null,
        rzTime: null,
        type: null,
        detail: null
      },
      projectCode: null,
      userId: null,
      // 表单参数
      form: {},
      userArr: [],
      projectArr: [],
      // 表单校验
      rules: {
        userId:[
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        projectCode:[
          { required: true, message: "项目不能为空", trigger: "blur" }
        ],
        rzTime:[
          { required: true, message: "动账时间不能为空", trigger: "blur" }
        ],
        detail:[
          { max: 255, message: '项目名称长度最多 255个字', trigger: 'blur' }
        ],
        money:[
          { required: true, message: "金额不能为空", trigger: "blur" },
          { pattern: /^([1-9]\d{0,7})(\.\d{1,2})?$/, message: '金额只能是数值最大为99999999,最多2位小数', trigger: 'blur' },
        ],
      }
    };
  },
  watch: {
    '$route': {
      deep: true,
      immediate: true,
      handler: function(val, oldVal) {
        if (val.name == 'Dzlog') {
          this.projectCode = this.$route.query.projectCode
          this.userId = this.$route.query.userId
          if (this.userId) {
            this.queryParams.userId = parseInt(this.userId)
            this.userId = parseInt(this.userId)
          }
          this.queryParams.projectCode = this.projectCode
          this.getXlk();
          this.getList();
        }
      }
    }
  },
  methods: {
    getXlk() {
      listDzProjectXlk().then(res => {
        this.projectArr = res.data;
        this.queryParams.projectCode = this.projectCode
      })
      listDzInfoXlk().then(res => {
        this.userArr = res.data;
      })
    },
    /** 查询对账记录列表 */
    getList() {
      this.loading = true;
      listDzLog(this.queryParams).then(response => {
        this.dzLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        szUserId: null,
        szUserName: null,
        projectCode: null,
        money: null,
        rzTime: null,
        type: '1',
        detail: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if (this.projectCode) {
        this.form.projectCode = this.projectCode;
      }
      if (this.userId) {
        this.form.userId = this.userId;
      }
      this.open = true;
      this.title = "添加对账记录";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.btnLoading = true
          let obj = this.userArr.filter(item => item.id === this.form.userId)[0]
          this.form.userName = obj.userName
          obj = this.userArr.filter(item => item.id === this.form.szUserId)[0]
          this.form.szUserName = obj.userName
          addDzLog(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(f => {
            this.btnLoading = false
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除对账记录编号为"' + ids + '"的数据项？').then(function() {
        return delDzLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    /** handleExport() {
      this.download('wms/dzLog/export', {
        ...this.queryParams
      }, `dzLog_${new Date().getTime()}.xlsx`)
    } */
  }
};
</script>
